<template>
  <el-dialog
    :title="!dataForm.commissionId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="团长id" prop="leaderId">
      <el-input v-model="dataForm.leaderId" placeholder="团长id"></el-input>
    </el-form-item>
    <el-form-item label="佣金类型" prop="commissionTypeId">
      <el-input v-model="dataForm.commissionTypeId" placeholder="佣金类型"></el-input>
    </el-form-item>
    <el-form-item label="所的佣金" prop="getCommission">
      <el-input v-model="dataForm.getCommission" placeholder="所的佣金"></el-input>
    </el-form-item>
    <el-form-item label="状态 0-待结算 1-已结算 2-已拒绝" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态 0-待结算 1-已结算 2-已拒绝"></el-input>
    </el-form-item>
    <el-form-item label="结算时间" prop="stockDaterTime">
      <el-input v-model="dataForm.stockDaterTime" placeholder="结算时间"></el-input>
    </el-form-item>
    <el-form-item label="当前订单状态 0-代发货 1-待团长收货 2-待用户收货 3-已退款 4-已完成" prop="orderState">
      <el-input v-model="dataForm.orderState" placeholder="当前订单状态 0-代发货 1-待团长收货 2-待用户收货 3-已退款 4-已完成"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          commissionId: 0,
          leaderId: '',
          commissionTypeId: '',
          getCommission: '',
          state: '',
          stockDaterTime: '',
          orderState: ''
        },
        dataRule: {
          leaderId: [
            { required: true, message: '团长id不能为空', trigger: 'blur' }
          ],
          commissionTypeId: [
            { required: true, message: '佣金类型不能为空', trigger: 'blur' }
          ],
          getCommission: [
            { required: true, message: '所的佣金不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态 0-待结算 1-已结算 2-已拒绝不能为空', trigger: 'blur' }
          ],
          stockDaterTime: [
            { required: true, message: '结算时间不能为空', trigger: 'blur' }
          ],
          orderState: [
            { required: true, message: '当前订单状态 0-代发货 1-待团长收货 2-待用户收货 3-已退款 4-已完成不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.commissionId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.commissionId) {
            this.$http({
              url: this.$http.adornUrl(`/leader/commission/info/${this.dataForm.commissionId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.leaderId = data.commission.leaderId
                this.dataForm.commissionTypeId = data.commission.commissionTypeId
                this.dataForm.getCommission = data.commission.getCommission
                this.dataForm.state = data.commission.state
                this.dataForm.stockDaterTime = data.commission.stockDaterTime
                this.dataForm.orderState = data.commission.orderState
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/leader/commission/${!this.dataForm.commissionId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'commissionId': this.dataForm.commissionId || undefined,
                'leaderId': this.dataForm.leaderId,
                'commissionTypeId': this.dataForm.commissionTypeId,
                'getCommission': this.dataForm.getCommission,
                'state': this.dataForm.state,
                'stockDaterTime': this.dataForm.stockDaterTime,
                'orderState': this.dataForm.orderState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
